from node import Node
from board import Board
from tile import Tile
from PlayedLetters import PlayedLetters
import inputbox
import sys
import pygame
from pygame.locals import *

'''
' Loads the word dictionary into a tree
'''
def load_dictionary(path):
    tree = Node()
    for line in open(path, 'r'):
        word = line.strip().lower()
        tree.add(word)
    return tree

def main():
    print 'Loading word list.'
    
    words = load_dictionary('words.txt')
    
    
    pygame.init()
    size = width, height = 1024, 768

    screen = pygame.display.set_mode(size)

    letters = inputbox.ask(screen, "Insert 25 letters").upper();
    while len(letters) != 25:
        letters = inputbox.ask(screen, "Try Again").upper();
    pygame.display.get_surface().fill(pygame.Color(255,255,255))
    pygame.display.flip()
    board = Board(letters)
    played_letters = PlayedLetters()
    anagrams = words.find_anagrams(letters)
    clock = pygame.time.Clock()
    
    #Game loop
    while True:
        clock.tick(60)
        
        #event checking
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEBUTTONDOWN:
                tile = board.click_tile(pygame.mouse.get_pos())
                if(tile != None):
                    played_letters.add(tile)
        board.update(screen)
        played_letters.update(screen)
        pygame.display.flip()

        #just for testing anagrams for now
        '''
        letters = raw_input('Enter letters: ')
        letters = letters.lower()
        letters = letters.replace(' ', '')
        if not letters:
            break
        count = 0
        for word in words.find_anagrams(letters):
            print word
            count += 1
        print '%d results.' % count
        '''

if __name__ == '__main__':
    print "Start"
    main()